package com.service.impl;

import com.dao.*;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.*;
import com.util.ExampleBuilder;
import com.util.PageData;
import com.util.PageWrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @ClassName ZixunServiceImpl
 * @Description 新闻资讯模块业务实现层
 */
@Service
public class ZixunServiceImpl implements ZixunService {

    @Autowired
    private ZixunMapper zixunMapper;



    //保存或更新新闻资讯表zixun
    @Override
    public Integer saveOrUpdate(Zixun instance) {
        if(null != instance.getId() && instance.getId() > 0) {
            zixunMapper.updateByPrimaryKeySelective(instance);
        }else {
            zixunMapper.insertSelective(instance);
        }
        return instance.getId();
    }

    //查询新闻资讯表zixun
    @Override
    public List<Zixun> findAll() {
        List<Zixun> list = zixunMapper.selectByExample(null);
        for (Zixun info:list) {
            setFatherInfo(info);
        }
        return list;
    }

    //根据条件(字符类型模糊匹配)查询新闻资讯表zixun
    @Override
    public List<Zixun> find(Zixun instance) {
        ZixunExample zixunExample = getZixunExampleByLike(instance);
        zixunExample.setOrderByClause(" id desc");
        List<Zixun> list = zixunMapper.selectByExample(zixunExample);
        for (Zixun info:list) {
            setFatherInfo(info);
        }
        return list;
     }

    //根据条件(字符类型完全匹配)查询新闻资讯表zixun
    @Override
    public List<Zixun> findByEqualTo(Zixun instance) {
        ZixunExample zixunExample = getZixunExampleByEqualTo(instance);
        zixunExample.setOrderByClause(" id desc");
        List<Zixun> list = zixunMapper.selectByExample(zixunExample);
        for (Zixun info:list) {
            setFatherInfo(info);
        }
        return list;
    }


    //根据条件(字符类型模糊匹配)查询新闻资讯表zixun最后一条数据
    @Override
    public Zixun findOne(Zixun instance) {
        List<Zixun> list = find(instance);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //根据条件(字符类型完全匹配)查询新闻资讯表zixun最后一条数据
    @Override
    public Zixun findOneByEqualTo(Zixun instance) {
        List<Zixun> list = findByEqualTo(instance);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //根据id列查询新闻资讯表zixun数据
    @Override
    public Zixun findById(Integer id) {
        Zixun info = zixunMapper.selectByPrimaryKey(id);
        setFatherInfo(info);
        return info;
    }

    //根据主键id列删除新闻资讯表zixun数据
    @Override
    public int deleteById(Integer id) {
        return zixunMapper.deleteByPrimaryKey(id);
    }


    //根据条件(字符类型模糊匹配)分页查询新闻资讯表zixun数据
    @Override
    public PageData<Zixun> findPage(PageWrap<Zixun> pageWrap) {
        PageHelper.startPage(pageWrap.getPageNum(), pageWrap.getPageSize());
        ZixunExample zixunExample = getZixunExampleByLike(pageWrap.getModel());
        zixunExample.setOrderByClause(pageWrap.getOrderByClause());
        List<Zixun> list = zixunMapper.selectByExample(zixunExample);
        for (Zixun info:list) {
            setFatherInfo(info);
        }
        return PageData.from(new PageInfo<>(list));
    }

    //根据条件(字符类型完全匹配)分页查询新闻资讯表zixun数据
    @Override
    public PageData<Zixun> findPageByEqualTo(PageWrap<Zixun> pageWrap) {
        PageHelper.startPage(pageWrap.getPageNum(), pageWrap.getPageSize());
        ZixunExample zixunExample = getZixunExampleByEqualTo(pageWrap.getModel());
        zixunExample.setOrderByClause(pageWrap.getOrderByClause());
        List<Zixun> list = zixunMapper.selectByExample(zixunExample);
        for (Zixun info:list) {
            setFatherInfo(info);
        }
        return PageData.from(new PageInfo<>(list));
    }

    //父表信息
    private void setFatherInfo(Zixun info){
        if (null != info){

        }
    }

    //字符类型模糊查询条件构造器
    private ZixunExample getZixunExampleByLike(Zixun instance) {
        ZixunExample zixunExample = new ZixunExample();
        ZixunExample.Criteria criteria = zixunExample.createCriteria();
        //新闻资讯id完全匹配
        if (null != instance.getId()) {
            criteria.andIdEqualTo(instance.getId());
        }
        //标题biaoti模糊匹配
        if (null != instance.getBiaoti() && !instance.getBiaoti().equals("")) {
            criteria.andBiaotiLike("%" + instance.getBiaoti() + "%");
        }
        //内容neirong模糊匹配
        if (null != instance.getNeirong() && !instance.getNeirong().equals("")) {
            criteria.andNeirongLike("%" + instance.getNeirong() + "%");
        }
        //图片image模糊匹配
        if (null != instance.getImage() && !instance.getImage().equals("")) {
            criteria.andImageLike("%" + instance.getImage() + "%");
        }
        //时间shijian模糊匹配
        if (null != instance.getShijian() && !instance.getShijian().equals("")) {
            criteria.andShijianLike("%" + instance.getShijian() + "%");
        }

        return zixunExample;
    }

    //字符类型完全相等查询条件构造器
    private ZixunExample getZixunExampleByEqualTo(Zixun instance) {
        ZixunExample zixunExample = new ZixunExample();
        ZixunExample.Criteria criteria = zixunExample.createCriteria();
        //新闻资讯id完全匹配
        if (null != instance.getId()) {
            criteria.andIdEqualTo(instance.getId());
        }
        //标题biaoti完全匹配
        if (null != instance.getBiaoti() && !instance.getBiaoti().equals("")) {
            criteria.andBiaotiEqualTo(instance.getBiaoti());
        }
        //内容neirong完全匹配
        if (null != instance.getNeirong() && !instance.getNeirong().equals("")) {
            criteria.andNeirongEqualTo(instance.getNeirong());
        }
        //图片image完全匹配
        if (null != instance.getImage() && !instance.getImage().equals("")) {
            criteria.andImageEqualTo(instance.getImage());
        }
        //时间shijian完全匹配
        if (null != instance.getShijian() && !instance.getShijian().equals("")) {
            criteria.andShijianEqualTo(instance.getShijian());
        }

        return zixunExample;
    }
}
